package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.BaseEntity;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Xiao
 * @date 2021/9/25
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    /**
     * 根据categoryId查询套餐数量
     * @param categoryId
     * @return
     */
    @Override
    public int getCountByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        return this.count(wrapper);
    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        /*for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.addSetmealDishs(setmealDishes);*/
        List<SetmealDish> list = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.addSetmealDishs(list);
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> getSetmealPage(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        wrapper.orderByDesc(BaseEntity::getUpdateTime);
        this.page(setmealPage, wrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<SetmealDto> records = setmealPage.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            String categoryName = categoryService.queryCategoryById(setmeal.getCategoryId()).getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(records);
        return setmealDtoPage;
    }

    /**
     * (批量)删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean deleteSetmeal(List<Long> ids) {
        List<Setmeal> setmeals = this.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus() == 1) {
                return false;
            }
        }
        setmealDishService.removeBySetmealIds(ids); //删除关联的菜品信息
        return this.removeByIds(ids);  //删除套餐数据
    }

    /**
     * (批量)修改状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public boolean setmealStatusByStatus(Integer status, List<Long> ids) {
        List<Setmeal> setmeals = this.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
        }
        return this.updateBatchById(setmeals);
    }

    /**
     * 根据id查找套餐
     * @param id
     * @return
     */
    @Override
    public SetmealDto querySetmealById(Long id) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = this.getOne(wrapper);
        List<SetmealDish> setmealDishes = setmealDishService.querySetmealDishsBySetmealId(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void editSetmeal(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        setmealDishService.removeBySetmealId(setmealDto.getId());
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes =setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.addSetmealDishs(setmealDishes);
    }

    /**
     * 根据分类id和状态查询套餐
     * @param setmeal
     * @return
     */
    @Override
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public List<SetmealDto> setmealListApi(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByDesc(BaseEntity::getUpdateUser);
        List<Setmeal> setmeals = this.list(wrapper);
        List<SetmealDto> setmealDtos = setmeals.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId, item.getId());
            List<SetmealDish> setmealDishes = setmealDishService.list(wrapper1);
            setmealDto.setSetmealDishes(setmealDishes);
            return setmealDto;
        }).collect(Collectors.toList());
        return setmealDtos;
    }

    /**
     * 根据套餐id查询套餐详情
     * @param setmealId
     * @return
     */
    @Override
    public List<SetmealDishDto> setMealDishDetailsApi(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        List<SetmealDishDto> setmealDishDtos = setmealDishes.stream().map(item -> {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtils.copyProperties(item, setmealDishDto);
            LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Dish::getId,item.getDishId());
            wrapper1.orderByDesc(Dish::getCreateTime);
            Dish dish = dishService.getOne(wrapper1);
            setmealDishDto.setImage(dish.getImage());
            return setmealDishDto;
        }).collect(Collectors.toList());
        return setmealDishDtos;
    }
}
